﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace lcs.Web.DemandPlan
{
    public partial class ListBidDepositConfirm : System.Web.UI.Page
    {
        Maticsoft.BLL.workFlow wm = new Maticsoft.BLL.workFlow();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Session["login"] != null)
                {
                    DataSet ds = Session["login"] as DataSet;

                    int person_id = Convert.ToInt32(ds.Tables[0].Rows[0]["user_id"]);

                    BindWSH(person_id);
                    BindYSH(person_id);
                }
            }
        }

        protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "bidconfrim")//保证金确认
            {
                int sid = Convert.ToInt32(e.CommandArgument);

                Maticsoft.BLL.supplier_bidplan sm = new Maticsoft.BLL.supplier_bidplan();

                Maticsoft.Model.supplier_bidplan s = sm.GetModel(sid);

                s.isBidDeposit = true;
                s.bidDepositTime = DateTime.Now;

                if (sm.Update(s))
                {
                    //获取当前行
                    GridViewRow gvr = (GridViewRow)((Control)e.CommandSource).Parent.Parent;

                    Label lbl_dpid = (Label)gvr.Cells[1].FindControl("lbl_dpid");

                    int dpid = Convert.ToInt32(lbl_dpid.Text);

                    SetNextFlowNode(dpid, s.submit_user_id.Value);

                    if (Session["login"] != null)
                    {
                        DataSet ds = Session["login"] as DataSet;

                        int person_id = Convert.ToInt32(ds.Tables[0].Rows[0]["user_id"]);

                        BindWSH(person_id);
                        BindYSH(person_id);
                    }

                    Response.Redirect("ListBidDepositConfirm.aspx");
                }



            }
        }

        //加入下一节点信息
        private void SetNextFlowNode(int dpid, int submitId)
        {
            List<Maticsoft.Model.documentFlowInfo> li = new Maticsoft.BLL.documentFlowInfo().GetModelList(" document_id = " + dpid);

            if (li.Count == 1)
            {
                Maticsoft.Model.documentFlowInfo d = li[0];

                string reason = "同意";
                int person_id = 0;
                if (Session["login"] != null)
                {
                    DataSet ds = Session["login"] as DataSet;

                    person_id = Convert.ToInt32(ds.Tables[0].Rows[0]["user_id"]);
                }

                //获取当前审批人审批节点号，transfer的undefine4记录的是该由哪个审批人审批的节点号，如果要修改自身的workFlow，则节点号就是undefine4
                int flow_node_num_now = Convert.ToInt32(d.flow_node_num_now);
                int flow_node_id_now = 0;

                flow_node_id_now = wm.GetFlow_node_id_now(person_id, d.document_id.Value, flow_node_num_now);

                if (flow_node_id_now != -1)
                {
                    if (wm.UpdateW(person_id, d.document_id.Value, 1, reason, DateTime.Now, flow_node_id_now, flow_node_num_now))//修改本身意见为同意,添加处理意见、处理时间
                    {
                        int flow_node_id = wm.GetFlow_node_id(person_id, d.document_id.Value, Convert.ToInt32(d.flow_node_num_now),d.flow_id.Value);

                        DataSet ds = wm.GetList(" flow_node_id = " + flow_node_id);

                        lcs.BLL.Tools.DealWorkFlowSend(d, flow_node_id, submitId, person_id);//发给商务

                    }

                }

            }
        }

        private void BindWSH(int person_id)
        {
            DataSet ds = new Maticsoft.BLL.supplier_bidplan().GetListByView(" isBidFee = 1  and isBidDeposit=0 and confirmBidDeposit_user_id=" 
                + person_id + " and isSendBidPlan = 1 ");

            this.GridView1.DataSource = ds;
            this.GridView1.DataBind();

        }

        private void BindYSH(int person_id)
        {
            DataSet ds = new Maticsoft.BLL.supplier_bidplan().GetListByView(" isBidDeposit=1 and confirmBidDeposit_user_id=" + person_id);

            this.GridView2.DataSource = ds;
            this.GridView2.DataBind();

        }

        protected void GridView2_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            this.GridView2.PageIndex = e.NewPageIndex;

            if (Session["login"] != null)
            {
                DataSet ds = Session["login"] as DataSet;

                int person_id = Convert.ToInt32(ds.Tables[0].Rows[0]["user_id"]);

                //BindWSH(person_id);
                BindYSH(person_id);
            }
        }
    }
}